package com.ybdz.scanner.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qmuiteam.qmui.widget.QMUIEmptyView;
import com.ybdz.scanner.R;
import com.ybdz.scanner.api.ApiStore;
import com.ybdz.scanner.base.BaseActivity;
import com.ybdz.scanner.bean.QueryBean;
import com.ybdz.scanner.event.ObjEvent;
import com.ybdz.scanner.utils.MyToast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @ClassName: QueryActivity
 * @author: zhao
 * @Description: 查询界面
 * @date 2018/2/24 16:14
 */
public class QueryActivity extends BaseActivity {

    @BindView(R.id.ll_back)
    LinearLayout mLlBack;
    @BindView(R.id.home)
    ImageView mHome;
    @BindView(R.id.name)
    TextView mName;
    @BindView(R.id.part)
    TextView mPart;
    @BindView(R.id.place)
    TextView mPlace;
    @BindView(R.id.breed)
    TextView mBreed;
    @BindView(R.id.rank)
    TextView mRank;
    @BindView(R.id.factory)
    TextView mFactory;
    @BindView(R.id.brand)
    TextView mBrand;
    @BindView(R.id.stockpile)
    TextView mStockpile;
    @BindView(R.id.specification)
    TextView mSpecification;
    @BindView(R.id.birth_date)
    TextView mBirthDate;
    @BindView(R.id.deposit_data)
    TextView mDepositData;
    @BindView(R.id.batch)
    TextView mBatch;
    @BindView(R.id.weight)
    TextView mWeight;
    @BindView(R.id.conformity)
    TextView mConformity;
    @BindView(R.id.back_home)
    Button mBackHome;
    @BindView(R.id.empty_query)
    QMUIEmptyView mEmptyQuery;

    private String result;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_queryresult);
        ButterKnife.bind(this);
        mEmptyQuery.show(true);
        EventBus.getDefault().register(this);
        initData(result);
    }

    /**
     * 网络请求
     * @param result
     */
    private void initData(final String result) {
        showLoading();
        ApiStore.getQuery(result).enqueue(new Callback<QueryBean>() {
            @Override
            public void onResponse(Call<QueryBean> call, Response<QueryBean> response) {
                if (response.isSuccessful() && response.body().getCode().equals("00")){
                    hideLoading();
                    init(response.body());
                }else {
                    hideLoading();
                    mEmptyQuery.show(false, "获取数据失败", null,null,null);
                }
            }

            @Override
            public void onFailure(Call<QueryBean> call, Throwable t) {
                hideLoading();
                mEmptyQuery.show(false, "加载不到数据了", null, "点击重试", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        initData(result);
                    }
                });
            }
        });
    }

    /**
     * 设置数据
     * @param mBean
     */
    private void init(QueryBean mBean) {
        if (mBean != null){
            String isQualified = mBean.getData().getIsQualified();
            String qualified = isQualified.equals("true") ? "是" : "否";
            mName.setText("品名：" + mBean.getData().getProductName());
            mPart.setText("部位名称：" + mBean.getData().getPart());
            mPlace.setText("原产地：" + mBean.getData().getAera());
            mBreed.setText("品种：" + mBean.getData().getBreed());
            mRank.setText("等级：" + mBean.getData().getGrade());
            mFactory.setText("工厂厂号：" + mBean.getData().getFactoryNumber());
            mBrand.setText("品牌：" + mBean.getData().getBrand());
            mStockpile.setText("储存方式：" + mBean.getData().getStorage());
            mSpecification.setText("规格：" + mBean.getData().getSize());
            mBirthDate.setText("生产日期：" + mBean.getData().getProductionDate());
            mDepositData.setText("入库日期：" + mBean.getData().getPutDate());
            mBatch.setText("产品批次：" + mBean.getData().getBatch());
            mWeight.setText("实际重量：" + mBean.getData().getWeight());
            mConformity.setText("是否合格：" + qualified);
            mEmptyQuery.hide();
        }else {
            mEmptyQuery.show(false,"获取不到数据",null,null,null);
        }


    }

    /**
     * 接收EventBus传递的数据
     *
     * @param responeEvent
     */
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void getQuery(ObjEvent responeEvent) {
        if (responeEvent.getId().equals("queryResult")) {
            result = (String) responeEvent.getObj();
        }

    }


    @OnClick({R.id.ll_back, R.id.home, R.id.back_home})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:              //返回
                finish();
                break;
            case R.id.home:                 //返回首页
                startActivity(new Intent(QueryActivity.this, MainActivity.class));
                finish();
                break;
            case R.id.back_home:            //返回首页
                startActivity(new Intent(QueryActivity.this, MainActivity.class));
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


}
